<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>errorhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_errorhandler.js.html">errorhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Error handling utilities.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.debug.ErrorHandler&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.debug&#39;);
<a name="line23"></a>goog.require(&#39;goog.debug.Trace&#39;);
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * The ErrorHandler can be used to to wrap functions with a try/catch
<a name="line27"></a> * statement. If an exception is thrown, the given error handler function will
<a name="line28"></a> * be called.
<a name="line29"></a> *
<a name="line30"></a> * @constructor
<a name="line31"></a> * @param {Function} handler Handler for exceptions.
<a name="line32"></a> */
<a name="line33"></a>goog.debug.ErrorHandler = function(handler) {
<a name="line34"></a>  /**
<a name="line35"></a>   * Handler for exceptions, which can do logging, reporting, etc.
<a name="line36"></a>   * @type {Function}
<a name="line37"></a>   * @private
<a name="line38"></a>   */
<a name="line39"></a>  this.errorHandlerFn_ = handler;
<a name="line40"></a>};
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Private helper function to return a span that can be clicked on to display
<a name="line45"></a> * an alert with the current stack trace. Newlines are replaced with a
<a name="line46"></a> * placeholder so that they will not be html-escaped.
<a name="line47"></a> * @param {string} stackTrace The stack trace to create a span for.
<a name="line48"></a> * @return {string} A span which can be clicked on to show the stack trace.
<a name="line49"></a> * @private
<a name="line50"></a> */
<a name="line51"></a>goog.debug.ErrorHandler.prototype.getStackTraceHolder_ = function(stackTrace) {
<a name="line52"></a>  var buffer = [];
<a name="line53"></a>  buffer.push(&#39;##PE_STACK_START##&#39;);
<a name="line54"></a>  buffer.push(stackTrace.replace(/(\r\n|\r|\n)/g, &#39;##STACK_BR##&#39;));
<a name="line55"></a>  buffer.push(&#39;##PE_STACK_END##&#39;);
<a name="line56"></a>  return buffer.join(&#39;&#39;);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Installs exception protection for an entry point function. When an exception
<a name="line62"></a> * is thrown from a protected function, a handler will be invoked to handle it.
<a name="line63"></a> *
<a name="line64"></a> * @param {Function} fn An entry point function to be protected.
<a name="line65"></a> * @param {boolean} opt_tracers Whether to install tracers around the fn.
<a name="line66"></a> * @return {!Function} A protected wrapper function that calls the entry point
<a name="line67"></a> *     function.
<a name="line68"></a> */
<a name="line69"></a>goog.debug.ErrorHandler.prototype.protectEntryPoint = function(fn,
<a name="line70"></a>                                                               opt_tracers) {
<a name="line71"></a>  var tracers = !!opt_tracers;
<a name="line72"></a>  var protectedFnName =
<a name="line73"></a>      &#39;__protected_&#39; + goog.getHashCode(this) + &#39;_&#39; + tracers + &#39;__&#39;;
<a name="line74"></a>  if (!fn[protectedFnName]) {
<a name="line75"></a>    fn[protectedFnName] = this.getProtectedFunction(fn, tracers);
<a name="line76"></a>  }
<a name="line77"></a>  return fn[protectedFnName];
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Helps {@link #protectEntryPoint} by actually creating the protected
<a name="line83"></a> * wrapper function, after {@link #protectEntryPoint} determines that one does
<a name="line84"></a> * not already exist for the given function.  Can be overriden by subclasses
<a name="line85"></a> * that may want to implement different error handling, or add additional
<a name="line86"></a> * entry point hooks.
<a name="line87"></a> * @param {Function} fn An entry point function to be protected.
<a name="line88"></a> * @param {boolean} tracers Whether to install tracers around fn.
<a name="line89"></a> * @return {!Function} protected wrapper function.
<a name="line90"></a> * @protected
<a name="line91"></a> */
<a name="line92"></a>goog.debug.ErrorHandler.prototype.getProtectedFunction = function(fn, tracers) {
<a name="line93"></a>  var that = this;
<a name="line94"></a>  if (tracers) {
<a name="line95"></a>    var stackTrace = goog.debug.getStacktraceSimple(15);
<a name="line96"></a>  }
<a name="line97"></a>  return function() {
<a name="line98"></a>    if (tracers) {
<a name="line99"></a>      var tracer = goog.debug.Trace.startTracer(&#39;protectedEntryPoint: &#39; +
<a name="line100"></a>        that.getStackTraceHolder_(stackTrace));
<a name="line101"></a>    }
<a name="line102"></a>    try {
<a name="line103"></a>      return fn.apply(this, arguments);
<a name="line104"></a>    } catch (e) {
<a name="line105"></a>      that.errorHandlerFn_(e);
<a name="line106"></a>      throw e;  // Re-throw it since this may be expected by the caller.
<a name="line107"></a>    } finally {
<a name="line108"></a>      if (tracers) {
<a name="line109"></a>        goog.debug.Trace.stopTracer(tracer);
<a name="line110"></a>      }
<a name="line111"></a>    }
<a name="line112"></a>  };
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Installs exception protection for window.setTimeout to handle exceptions.
<a name="line118"></a> * @param {boolean} opt_tracers Whether to install tracers around the fn.
<a name="line119"></a> */
<a name="line120"></a>goog.debug.ErrorHandler.prototype.protectWindowSetTimeout =
<a name="line121"></a>    function(opt_tracers) {
<a name="line122"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line123"></a>  var originalSetTimeout = win.setTimeout;
<a name="line124"></a>  var that = this;
<a name="line125"></a>  win.setTimeout = function(fn, time) {
<a name="line126"></a>    // IE doesn&#39;t support .call for setTimeout, but it also doesn&#39;t care
<a name="line127"></a>    // what &quot;this&quot; is, so we can just call the original function directly
<a name="line128"></a>    fn = that.protectEntryPoint(fn, opt_tracers);
<a name="line129"></a>    if (originalSetTimeout.call) {
<a name="line130"></a>      return originalSetTimeout.call(this, fn, time);
<a name="line131"></a>    } else {
<a name="line132"></a>      return originalSetTimeout(fn, time);
<a name="line133"></a>    }
<a name="line134"></a>  };
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Install exception protection for window.setInterval to handle exceptions.
<a name="line140"></a> * @param {boolean} opt_tracers Whether to install tracers around the fn.
<a name="line141"></a> */
<a name="line142"></a>goog.debug.ErrorHandler.prototype.protectWindowSetInterval =
<a name="line143"></a>    function(opt_tracers) {
<a name="line144"></a>  var win = goog.getObjectByName(&#39;window&#39;);
<a name="line145"></a>  var originalSetInterval = win.setInterval;
<a name="line146"></a>  var that = this;
<a name="line147"></a>  win.setInterval = function(fn, time) {
<a name="line148"></a>    // IE doesn&#39;t support .call for setInterval, but it also doesn&#39;t care
<a name="line149"></a>    // what &quot;this&quot; is, so we can just call the original function directly
<a name="line150"></a>    fn = that.protectEntryPoint(fn, opt_tracers);
<a name="line151"></a>    if (originalSetInterval.call) {
<a name="line152"></a>      return originalSetInterval.call(this, fn, time);
<a name="line153"></a>    } else {
<a name="line154"></a>      return originalSetInterval(fn, time);
<a name="line155"></a>    }
<a name="line156"></a>  };
<a name="line157"></a>};
</pre>


</body>
</html>
